var elementObj;
var objVal;
/*
 * Get the category value, via Ajax call 
 */
function getKategory(obj, elementObjToChange) {
	var req = newXMLHttpRequest();
	objVal = obj.value;
	
	req.onreadystatechange = getReadyStateHandler(req, parseResult);

	req.open("POST", "ajaxTestInfo.do", true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send("cmd=ajaxTestAct&catgId=" + objVal);
	elementObj = elementObjToChange;
	
}

function setFisrtTwoOtions(objVal) {
	
	if (objVal == 1)
    {
    	document.forms[0].categoryVal.options[0] = new Option('--Select--','0');
		document.forms[0].categoryVal.options[1] = new Option('--Wildcard--','-1');
		count = 2;
	}
}
/**
 * Update value area of page to reflect contents of category value
 * described in XML document.
 */
function parseResult(req) {
	responseXML = req.responseXML;
	var count = 0;
	if(!(elementObj==null || elementObj.options == null)){
		clearSelectionBox(elementObj);
		var allNodes = responseXML.getElementsByTagName("kategories")[0].childNodes;
	
		if (objVal == 1)
	    {
			elementObj.options[0] = new Option('--SELECT--','0');
			elementObj.options[1] = new Option('--WILDCARD--','-1');
			count = 2;
			loadDate(elementObj,allNodes,count);
		}
		else if (objVal == 0)
	    {
			elementObj.options[0] = new Option('--SELECT--','0');
			count = 1;
			//loadDate(elementObj,allNodes,count);
		}
		else
		{
			elementObj.options[0] = new Option('--SELECT--','0');
			count = 1;
			loadDate(elementObj,allNodes,count);
		}
	}
}
 /*--------------------------------------Drop Down FIlter-------------------------------------------------*/
 function closeDivs(obj,divName)
 {
 	var catgId = obj.value;
 	if (catgId != 1)
 	{
 		closeDiv(divName);
 	}
 }	
 /*-----------------------------------------------------------------------------------------------------------*/

 function loadDate(elementObj,allNodes,count)
 {
	 for ( var i = 0; i < allNodes.length ; i++) 
		{
			elementObj.options[i+count] = new Option(
					allNodes[i].childNodes[0].nodeValue,
					allNodes[i].childNodes[0].nodeValue);
		}
 }
 
 function chkWildCard(paramVal,clearFlag)
	{
		var catId=document.forms[0].categoryId.value;
		if(null!=paramVal && paramVal== -1 && null!=catId && catId ==1 )
		{		
			if(clearFlag == 0)
			{
				document.forms[0].wildCardPartName.value = '';	
			}
			openDiv('wildCardPartDiv');
		}
		else
		{
			closeDiv('wildCardPartDiv');
		}
	}

	function chkWildCardExclude(paramVal,clearFlag)
	{
		var catId=document.forms[0].categoryIdExclude.value;
		if(null!=paramVal && paramVal== -1 && null!=catId && catId ==1 )
		{		
			if(clearFlag == 0)
			{
				document.forms[0].wildCardPartNameExclude.value = '';	
			}
			openDiv('wildCardPartDivExc');
		}
		else
		{
			closeDiv('wildCardPartDivExc');
		}
	}
	
	function displayFilteredGraphs(catId,catVal,catIdEx,catValEx,tmpWildCard,tmpWildCardExclude)
	{
		if (catId == 0 && catIdEx == 0)
		{
			//alert("Please select Filter Criteria");
			return true;
		}
		else if (catId == 0 && catIdEx != 0)
		{
			
			if(criteria(catValEx,tmpWildCardExclude))
			{
				return true;
			}
		}
		else if (catId != 0 && catIdEx == 0)
		{
			if(criteria(catVal,tmpWildCard))
			{
				return true;
			}
			
		}
		else 
		{
			if(criteria(catVal,tmpWildCard) && criteria(catValEx,tmpWildCardExclude))
			{
				return true;
			}
			
		}
		return false;
	}
	function criteria(catVal,tmpWildCard)
	{
		if (catVal == -1)
		{
			if(tmpWildCard == '')
			{
				alert("Wild card field cannot be empty");
				return false;
			}
		}
		else if (catVal == 0)
		{
			alert("Please select Filter Criteria");
			return false;
		}
		
		return true;
	}